<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('执行日志')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>表名：</label>
                                <input type="text" name="jobDesc"/>
                            </li>
                            <li>
                                <label>调度结果：</label>
                                <select name="handleCode">
                                    <option value="">全部</option>
                                    <option value="200">成功</option>
                                    <option value="500">失败</option>
                                    <option value="0">抽取中</option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                                <a class="btn btn-danger btn-rounded btn-sm" onclick="toRemove()"><i class="fa fa-remove"></i>&nbsp;删除</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
     <div class="modal inmodal fade" id="myModal5" tabindex="-1" role="dialog" aria-hidden="true">
         <div class="modal-dialog modal-lg">
             <div class="modal-content">
                 <div class="modal-header">
                     <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                     </button>
                     <h4 class="modal-title">调度日志</h4>
                 </div>
                 <div class="modal-body">
                     <p id="log"></p>
                 </div>
             </div>
         </div>
     </div>
     <div class="modal inmodal fade" id="myModal6" tabindex="-1" role="dialog" aria-hidden="true" >
         <div class="modal-dialog modal-lg">
             <div class="modal-content">
                 <div class="modal-header">
                     <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                     </button>
                     <h4 class="modal-title">删除日志</h4>
                 </div>
                 <div class="modal-body">
                     <input type="hidden" id="execType"/>
                     <div class="form-group" style="width: 90%">
                         <label class="col-sm-3 font-noraml is-required;" style="text-align-last: right;vertical-align: center">时间范围：</label>
                         <div class="col-sm-8">
                             <select id="deleteDateType" class="form-control" type="text">
                                 <option value="weekAgo">一周前</option>
                                 <option value="monthAgo">一月前</option>
                                 <option value="">全部</option>
                             </select>
                         </div>
                     </div>
                 </div>
                 <div class="modal-footer">
                     <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                     <button type="button" class="btn btn-primary" onclick="remove()">确定</button>
                 </div>
             </div>
         </div>
     </div>
     <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var templateData = [[${@jobTemplateService.selectJobTemplateList({})}]];
        var prefix = ctx + "base/job/log";
        function toRemove() {
            $("#myModal6").modal("show");
        }
        function remove() {
            var type = $("#deleteDateType").val();
            $.ajax({
                url: prefix+"/remove?dateType="+type,
                type: 'GET',
                success: function (result) {
                    if(result.code==0) {
                        $.table.refresh();
                        $("#myModal6").modal("hide");
                    } else {
                        $.modal.alertError(result.msg);
                    }
                }
            });
        }
        function showTriggerMsg(id,type) {
            $.ajax({
                url: prefix+"/selectById?logId="+id,
                type: 'GET',
                success: function (result) {
                    if(result.code==0) {
                        var data = result.data;
                        if(type=='trigger') {
                            $("#log").html(data.triggerMsg);
                        } else {
                            $("#log").html(data.handleMsg);
                        }
                        $("#myModal5").modal("show");
                    }
                }
            });
        }
        function showLog(id) {
            $.ajax({
                url: prefix+"/logDetailCat?logId="+id,
                type: 'GET',
                success: function (result) {
                    if(result.code==0) {
                        var data = result.data;
                        $("#log").html(data.logContent);
                        $("#myModal5").modal("show");
                    }
                }
            });
        }
        $(function() {
            var options = {
                url: prefix + "/list",
                modalName: "执行日志",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'param',
                    title: '日期范围'
                },
                {
                    field: 'jobTemplate',
                    title: '定时模板',
                    formatter: function(value, item, index) {
                        let name='不存在';
                        $.each(templateData,function (i,t) {
                            if(value==t.id) {
                                name = t.jobDesc;
                                return false;
                            }
                        });
                        return name;
                    }
                },
                {
                    field: 'jobCode',
                    title: '表'
                },
                {
                    field: 'jobDesc',
                    title: '任务描述'
                },
                {
                    field: 'triggerCode',
                    title: '调度结果',
                    formatter: function(value, row, index) {
                        if(row.triggerCode==200) {
                            return '成功';
                        } else if(row.triggerCode>200) {
                            return '失败';
                        } else {
                            return '进行中';
                        }
                    }
                },
                {
                    field: '',
                    title: '调度备注',
                    align: 'center',
                    formatter: function(value, row, index) {
                        if(row.triggerCode==200) {
                            return '<a class="btn btn-default btn-xs " href="javascript:void(0)" onclick="showTriggerMsg(\'' + row.id + '\',\'trigger\')"><i class="fa fa-eye"></i>查看</a>';
                        } else {
                            return '<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick="showTriggerMsg(\'' + row.id + '\',\'trigger\')"><i class="fa fa-eye"></i>查看</a>';
                        }
                    }

                },
                {
                    field: 'handleTime',
                    title: '执行时间'
                },
                {
                    field: 'handleCode',
                    title: '执行结果',
                    formatter: function(value, row, index) {
                        if(row.handleCode==200) {
                            return '成功';
                        } else if(row.handleCode>200) {
                            return '失败';
                        } else {
                            return '进行中';
                        }
                    }
                },
                    {
                        field: 'tiems',
                        title: '耗时（秒）',
                        formatter: function(value, row, index) {
                            debugger;
                            var begin = new Date(row.triggerTime).getTime();
                            var end = new Date(row.handleTime).getTime();
                            if(begin==0 || end==0) {
                                return 0;
                            } else {
                                return (end-begin)/1000;
                            }
                        }
                    },
                {
                    field: '',
                    title: '执行备注',
                    align: 'center',
                    formatter: function(value, row, index) {
                        if(row.handleCode==200) {
                            return '<a class="btn btn-default btn-xs " href="javascript:void(0)" onclick="showTriggerMsg(\'' + row.id + '\')"><i class="fa fa-eye"></i>查看</a>';
                        } else {
                            return '<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick="showTriggerMsg(\'' + row.id + '\')"><i class="fa fa-eye"></i>查看</a>';
                        }
                    }

                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs " href="javascript:void(0)" onclick="showLog(\'' + row.id + '\')"><i class="fa fa-edit"></i>日志</a> ');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
    </script>
</body>
</html>